package com;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;


import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.topway.dsmflex.Log;


/**
 * 
 * @author liusy 2013-04-28
 *
 */
public class AppContext {
	
   private static Logger log = Logger.getLogger(AppContext.class);
   
   public static String  FileRootPath;
   public static String  IndexPath;
   
   
   public static void init() {
	  Log.info("系统  初始化...");
      PropertyConfigurator.configure(System.getProperty("user.dir") + "/log4j.properties");
      Properties props = readProperties(System.getProperty("user.dir") + "/config.properties");
      FileRootPath = props.getProperty("path.fileRoot");
      IndexPath = props.getProperty("path.fullIndex");
      Log.info("系统  初始化结束.");
   }

   public static Properties readProperties(String filePath) {
      Properties props = new Properties();
      try {
         InputStream in = new BufferedInputStream(new FileInputStream(filePath));
         props.load(in);
      }
      catch (Exception e) {
         e.printStackTrace();
      }
      return props;
   }

   //写入properties信息
   public static void writeProperties(String filePath, String parameterName, String parameterValue) {
      Properties prop = new Properties();
      try {
    	  File file = new File(filePath);
    	  if(!file.exists())
    		  file.createNewFile();
         InputStream fis = new FileInputStream(filePath);
         //从输入流中读取属性列表（键和元素对）
         prop.load(fis);
         //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
         //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
         OutputStream fos = new FileOutputStream(filePath);
         prop.setProperty(parameterName, parameterValue);
         //以适合使用 load 方法加载到 Properties 表中的格式，
         //将此 Properties 表中的属性列表（键和元素对）写入输出流
         prop.store(fos, "Update '" + parameterName + "' value");
      }
      catch (IOException e) {
         System.err.println("Visit " + filePath + " for updating " + parameterName + " value error");
      }
   }
   
  public static void main(String[] args) {
	  System.out.println(1000*60*0.1);
	  
	  Calendar c = Calendar.getInstance();
	  c.setTimeInMillis(System.currentTimeMillis());
	  System.out.println(System.currentTimeMillis());
	  DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS.ms");
	  df.setCalendar(c);
	  
	  System.out.println(df.format(c.getTime()));
	  
	  for (int i = 0; i < 1000; i++) {
		  c.setTimeInMillis(System.currentTimeMillis());
		  System.out.println(df.format(c.getTime()));
	  }
	  
  }
	
}
